<?php
class Am_Grid_Filter_Report extends Am_Grid_Filter_Abstract
{
    protected $title;
    public function __construct()
    {
        $this->title = ___("Filter By Site/Url Link/Profile Page:");
        parent::__construct();
    }
    public function getVariablesList()
    {
        $ret = parent::getVariablesList();
        $ret[] = 'search';
        $ret[] = 'search_load';
        return $ret;
    }
    protected function applyFilter()
    {
        // done in initFilter
    }
  
   
    public function renderInputs()
    {
        return $this->renderInputText('filter');
    }
    public function initFilter(Am_Grid_ReadOnly $grid)
    {
        parent::initFilter($grid);
        $query = $grid->getDataSource();
       // $query->setPrefix('_u_search');
        /* @var $query Am_Query_User */
        if ($id = $grid->getRequest()->getInt('search_load')){
            $query->load($id);
        } elseif (is_string($this->vars['filter']) && $this->vars['filter']){
            $cond = new Am_Query_Report_Condition_Filter();
            $cond->setFromRequest(array('filter' => array('val' => $this->vars['filter'])));
            $query->add($cond);
        } else {
            $query->setFromRequest($grid->getCompleteRequest());
        }
    }
    public function isFiltered()
    {
        return (bool)$this->grid->getDataSource()->getConditions();
    }
}

class AdminJobsReportController extends Am_Controller_Grid
{
    public function preDispatch()
    {
        parent::preDispatch();
      
    }
   
    
    public function checkAdminPermissions(Admin $admin)
    {
        return $admin->hasPermission('grid_r');
    }
    
    public function createGrid()
    {
		
   		
		$ds = new Am_Query_Report;
		$ds->innerJoin('?_job', 'j', " r.job_id_server = j.job_id_server");
		$ds->addField('j.job_id');
		
		
	
		
		$ds->addWhere("j.job_id = ?", $this->getParam('jobid'));
		$ds->setOrder("id");
		
		
		
        $grid = new Am_Grid_Editable('_r', ___("Browse Reports"), $ds, $this->_request, $this->view);
		
		$grid->addGridField(new Am_Grid_Field('id', 'ID', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('site', 'Site', true, '', null, null));
		
		
		
	
	
		$grid->addGridField(new Am_Grid_Field('url_link', 'URL Link', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('profile_page', 'Profile Page', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('vote', 'Vote', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('status', ___('Status'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('date_time', ___('Time'), true, '', null, null));
		
		$grid->addGridField(new Am_Grid_Field('google_pr', 'Google PR', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('alexa_pr', 'Alexa PR', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('google_index', 'Google Index', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('last24hours', ___('Last 24 Hours'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('lastweek', ___('Last Week'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('lastmonth', 'Last Month', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('yahoo_link', 'Yahoo Link', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('submit_captcha', 'Submit Captcha', true, '', null, null));
	
	
		$grid->actionsClear();
		
		$grid->actionAdd($this->createActionExport());	
		$grid->actionAdd($this->createActionCustomize());
		$grid->setFilter(new Am_Grid_Filter_Report());		
		//$actTest = new Am_Grid_Action_Url('report', ___('View Report this Job'), '__ROOT__/usercpjobsreport?id=__ID__', '_blank');
        return $grid;
    }
	protected function createActionCustomize() {
        $action = new Am_Grid_Action_Customize();
		$action->setUser(false);
       	$action->addField(new Am_Grid_Field('id', ___('Id')))
		 	->addField(new Am_Grid_Field('site', ___('Site')))
			->addField(new Am_Grid_Field('url_link', ___('Url link')))
            ->addField(new Am_Grid_Field('profile_page', ___('Profile Page')))
			->addField(new Am_Grid_Field('vote', ___('Vote')))
            ->addField(new Am_Grid_Field('status', ___('Status')))
			->addField(new Am_Grid_Field('date_time', ___('Time')))
			->addField(new Am_Grid_Field('google_pr', ___('Google Pr')))
			->addField(new Am_Grid_Field('alexa_pr', ___('Alexa Pr')))
			->addField(new Am_Grid_Field('google_index', ___('Google Index')))
			->addField(new Am_Grid_Field('last24hours', ___('Last 24 Hours')))
			->addField(new Am_Grid_Field('lastweek', ___('Last Week')))
			->addField(new Am_Grid_Field('lastmonth', 'Last Month'))
			->addField(new Am_Grid_Field('yahoo_link', 'Yahoo Link'))
			->addField(new Am_Grid_Field('submit_captcha', 'Submit Captcha'));
         
            //Additional Fields
        foreach ($this->getDi()->autovoteTable->customFields()->getAll() as $field) {
            if (isset($field->from_config) && $field->from_config) {
                if ($field->sql) {
                    $action->addField(new Am_Grid_Field($field->name, $field->title));
                } else {
                    $action->addField(new Am_Grid_Field_Data($field->name, $field->title));
                }
            }
        }

        return $action;
    }
	 protected function createActionExport() {
        $action = new Am_Grid_Action_Export();
		$action->setUser(false);
		$action->setJobId($this->getParam('jobid'));
         $action->addField(new Am_Grid_Field('id', ___('Id')))
		 	->addField(new Am_Grid_Field('site', ___('Site')))
			->addField(new Am_Grid_Field('url_link', ___('Url link')))
            ->addField(new Am_Grid_Field('profile_page', ___('Profile Page')))
			->addField(new Am_Grid_Field('vote', ___('Vote')))
            ->addField(new Am_Grid_Field('status', ___('Status')))
			->addField(new Am_Grid_Field('date_time', ___('Time')))
			->addField(new Am_Grid_Field('google_pr', ___('Google Pr')))
			->addField(new Am_Grid_Field('alexa_pr', ___('Alexa Pr')))
			->addField(new Am_Grid_Field('google_index', ___('Google Index')))
			->addField(new Am_Grid_Field('last24hours', ___('Last 24 Hours')))
			->addField(new Am_Grid_Field('lastweek', ___('Last Week')))
			->addField(new Am_Grid_Field('lastmonth', 'Last Month'))
			->addField(new Am_Grid_Field('yahoo_link', 'Yahoo Link'))
			->addField(new Am_Grid_Field('submit_captcha', 'Submit Captcha'));
			
        
        //Additional Fields
		//var_dump($this->getDi()->UserTable->customFields()->getAll()); die();
        foreach ($this->getDi()->autovoteTable->customFields()->getAll() as $field) {
            if (isset($field->from_config) && $field->from_config) {
                if ($field->sql) {
                    $action->addField(new Am_Grid_Field($field->name, $field->title));
                } else {
                    //we use trailing __ to distinguish fields from data table
                    $action->addField(new Am_Grid_Field($field->name . '__', $field->title));
                }
            }
        }
        
      //  $action->setGetDataSourceFunc(array($this, 'getDS'));
        return $action;
    }
   
    

  
  
}
